package demo;

import org.junit.Test;

/**
 * ThreadDemo
 *
 * @author chisheng
 */
public class ThreadDemo {

    /* A thread is a thread of execution in a program.
     * The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. */

    @Test
    public void testCreateThread() {
        /* public Thread()
         *  Allocates a new Thread object.
         *  This constructor has the same effect as Thread (null, null, gname),
         *  where gname is a newly generated name.
         *  Automatically generated names are of the form "Thread-"+n, where n is an integer. */
        final MyThread thread0 = new MyThread();
        final MyThread thread1 = new MyThread();
        final MyThread thread2 = new MyThread();

        /* public synchronized void start()
         *  Causes this thread to begin execution;
         *  the Java Virtual Machine calls the run method of this thread.
         *  The result is that two threads are running concurrently:
         *  the current thread (which returns from the call to the start method)
         *  and the other thread (which executes its run method). */
        thread0.start();
        thread1.start();
        thread2.start();
    }

    private static class MyThread extends Thread {
        @Override
        public void run() {
            /* public final String getName()
             *  Returns this thread's name. */
            System.out.println(this.getName() + " is running now.");
        }
    }

}
